﻿#include "unstackplateinfo.h"
#include <QTimer>
#include <QMessageBox>

UnstackPlateInfo::UnstackPlateInfo(UnstackPlate* point, QWidget *parent)
    : m_plate(point), QWidget(parent)
{
    ui.setupUi(this);

	ui.label->setText(m_plate->GetCameraId());
	ShowPicture();
}

UnstackPlateInfo::~UnstackPlateInfo()
{
}

void UnstackPlateInfo::on_pushButton_clicked()
{
	QString rgb, depth;
	if (m_plate->SnapPicture(rgb, depth)) {
		QPixmap pixmap(rgb);
		if (!pixmap.isNull()) {
			ui.label_4->setPixmap(pixmap.scaled(ui.label_4->size(), Qt::KeepAspectRatio));
			m_pic.visPath = rgb;
		}
	} else {
		QMessageBox::warning(this, "WARRING", "拍照失败");
	}
}

void UnstackPlateInfo::ShowPicture()
{
	auto pic = m_plate->GetCurretPicture();
	if (pic.visPath != m_pic.visPath ) {
		// 可选：加载图片到 QLabel
		QPixmap pixmap(pic.visPath);
		if (!pixmap.isNull()) {
			ui.label_4->setPixmap(pixmap.scaled(ui.label_4->size(), Qt::KeepAspectRatio));
			m_pic.visPath = pic.visPath;
			m_pic.rgbPath = pic.rgbPath;
		}
	} else {
		if (pic.rgbPath != m_pic.rgbPath) {
			QPixmap pixmap(pic.rgbPath);
			if (!pixmap.isNull()) {
				ui.label_4->setPixmap(pixmap.scaled(ui.label_4->size(), Qt::KeepAspectRatio));
				m_pic.rgbPath = pic.rgbPath;
			}
		}
	}

	if (pic.algPath != m_pic.algPath) {
		QPixmap pixmap(pic.algPath);
		if (!pixmap.isNull()) {
			ui.label_5->setPixmap(pixmap.scaled(ui.label_5->size(), Qt::KeepAspectRatio));
			m_pic.algPath = pic.algPath;
		}
	}

	if (pic.algPath.isEmpty()) {
		ui.label_5->setPixmap(QPixmap());
		m_pic.algPath = pic.algPath;
	}
	QTimer::singleShot(1000, this, &UnstackPlateInfo::ShowPicture);
}
